<?php
class User extends AppModel {

	var $name = 'User';
	var $ncount = 0;
	var $dis = array();
	var $ncelulas = 0;
	var $tipoDescendencia = 12;
	var $bfirst = true;
	//The Associations below have been created with all possible keys, those that are not needed can be removed
	var $belongsTo = array(
		'Iglesia' => array(
			'className' => 'Iglesia',
			'foreignKey' => 'iglesia_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Oficio' => array(
			'className' => 'Oficio',
			'foreignKey' => 'oficio_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		),
		'Sector' => array(
			'className' => 'Sector',
			'foreignKey' => 'sector_id',
			'conditions' => '',
			'fields' => '',
			'order' => ''
		)
	);

	var $hasMany = array(
		'Rol' => array(
			'className' => 'Rol',
			'foreignKey' => 'user_id',
			'dependent' => false,
			'conditions' => '',
			'fields' => '',
			'order' => '',
			'limit' => '',
			'offset' => '',
			'exclusive' => '',
			'finderQuery' => '',
			'counterQuery' => ''
		)
	);
	var $validate = array(
		'iglesia_id'=> array(
						'rule' 		=> 	'notEmpty',
						'required' 		=> 	true,
						'message' 	=>	'El identificador de Iglesia no puede ser núlo.'
					),
		'oficio_id'=> array(
						'rule' 		=> 	'notEmpty',
						'required' 		=> 	true,
						'message' 	=>	'El identificador de Oficio no puede ser núlo.'
					),
		'sector_id'=> array(
						'rule' 		=> 	'notEmpty',
						'required' 		=> 	true,
						'message' 	=>	'El identificador de Sector no puede ser núlo.'
					)								
	);
	function getCountGrid($like){
		if($like!=null)
			$condicion = " AND ".$like." ";
		else $condicion = "";
		$res = $this->query("
			SELECT	count(user.id)	as count					
			FROM	users user, iglesias iglesia, sectors sector
			WHERE	user.iglesia_id = iglesia.id
			AND		user.sector_id = sector.id 
			$condicion");	
		return $res[0][0]["count"];
	}
	function getUsersGrid($orden,$limit,$like){
		if($like!=null)
			$condicion = " AND ".$like." ";
		else $condicion = "";
		$res = $this->query("
			SELECT  user.id, user.rut, user.nombre1, user.nombre2, user.apellido1, user.apellido2, user.sexo,
			iglesia.nombre, user.direccion, user.fono_fijo, user.fono_movil, 
			user.email, user.created, user.modified, user.proceso, sector.nombre					
			FROM	users user, iglesias iglesia , sectors sector
			WHERE	iglesia.id = user.iglesia_id
			AND		user.sector_id = sector.id
			$condicion
			ORDER BY 	$orden  LIMIT $limit;");	
		return $res;
	}
	function getUsersByGrupo($orden,$limit,$like,$gid){
		if($like!=null)
			$condicion = " AND ".$like." ";
		else $condicion = "";
		$res = $this->query("
			SELECT  tiporol.nombre, user.id, user.nombre1, user.nombre2, user.apellido1, user.apellido2,
			user.fono_fijo, user.fono_movil, user.email, user.created, user.proceso					
			FROM	users user, rols rol, tiporols tiporol 
			WHERE	rol.grupo_id = $gid
			AND		rol.user_id = user.id
			AND		rol.tiporol_id = tiporol.id
			$condicion
			ORDER BY 	$orden  LIMIT $limit;");	
		return $res;
	}
	function yaExiste($data){
		if(!empty($data["User"]["email"])):
			$email = $this->find('first',array(
							"conditions"=>array(
								"LOWER(User.email)"=>strtolower($data["User"]["email"])
							),
							'recursive' => -1
						)
			);
			if(!empty($email)):
				return true;
			else:
				return false;
			endif;	
		endif;
		/*d			
		if(!empty($data["User"]["fono_movil"])):
		
			$fono = $this->find('first',array(
							"conditions"=>array(
								"User.fono_movil"=>$data["User"]["fono_movil"],
								"User.nombre1"=>$data["User"]["nombre1"]
							),
							'recursive' => -1
						)
			);
			if(!empty($fono)):
				return true;
			else:
				return false;
			endif;	
		endif;
		
		
		$user = $this->find('first',array(
							"conditions"=>array(
								"LOWER(User.nombre1)"=>strtolower($data["User"]["nombre1"]),
								"LOWER(User.nombre2)"=>strtolower($data["User"]["nombre2"]),
								"LOWER(User.apellido1)"=>strtolower($data["User"]["apellido1"]),
								"LOWER(User.apellido2)"=>strtolower($data["User"]["apellido2"])
							),
							'recursive' => -1
						)
			);
		if(!empty($user)):
			return true;
		else:
			return false;
		endif;
		*/
	}
	function getMiracleData($uid,$tipo=null){
		$celula = $this->query("
			select  r.grupo_id as celula_id
			from	rols r, tiporols tp, tipogrupos tpg, grupos g
			where	tp.nombrerol = 'lider'
			and	tpg.nombretipo = 'celula'
			and	r.user_id = ".$uid."
			and	r.tiporol_id = tp.id
			and	r.grupo_id = g.id
			and	g.tipogrupo_id = tpg.id
		");	
		if(!empty($celula)):
			$cid = $celula[0]["r"]["celula_id"];
			$this->ncelulas = $this->ncelulas+1;
			$dis = $this->query("
				select  r.user_id as user_id
				from	rols r, tiporols tp
				where	r.grupo_id = ".$cid."
				and	r.tiporol_id = tp.id
				and	tp.nombrerol = 'discipulo'
			");	
			$this->ncount = $this->ncount+count($dis);
			if($tipo!=null)	$this->tipoDescendencia = $tipo*12;
			
			
			if(!empty($this->dis[$this->tipoDescendencia]))
				$this->dis[$this->tipoDescendencia] = $this->dis[$this->tipoDescendencia]+count($dis);
			else
				$this->dis[$this->tipoDescendencia] = 	count($dis);
			if(!empty($dis)):
				foreach($dis as $dat):
					$this->getMiracleData($dat["r"]["user_id"], $this->tipoDescendencia);
				endforeach;
				$this->tipoDescendencia = $this->tipoDescendencia/12;
				if($this->tipoDescendencia == 1) $this->tipoDescendencia = 12; 
			endif;
		endif;
	}
}
?>